#include "median.h"

int** madian_fil(int** A, int** mask, int M, int N, int K, int L)
{
	int** mass = NULL;
	int** tmp;
	tmp = new int*[K];
	for (int i = 0; i < K; i++)
	{
		tmp[i] = new int[L];
		for (int j = 0; j < L; j++)
		{
			tmp[i][j] = 0;
		}
	}
	int temp2 = 0;
	int lowest = A[0][0];
	int unnormal;
	bool debug = true;
	for (int i = 0; i < M; i++)
	{
		for (int j = 0; j < N; j++)
		{
			if (A[i][j] < lowest)
				lowest = A[i][j];
		}
	}
	mass = new int*[M];
	for (int i = 0; i < M; i++)
	{
		mass[i] = new int[L];
		for (int j = 0; j < N; j++)
		{
			if (A[i][j] > 100 || A[i][j] < 15)
			{
				__try
				{
					tmp[0][0] = A[i - 1][j - 1];
					if (tmp[0][0] < -3000)
						tmp[0][0] = 0;
				}
				__except (EXCEPTION_EXECUTE_HANDLER)
				{
					DWORD err = GetExceptionCode();
					if (err == EXCEPTION_ACCESS_VIOLATION)
					{
						tmp[0][0] = 0;
						//std::cout << 111;
						//continue;

					}
					//}
				}
				__try
				{
					tmp[0][1] = A[i - 1][j];
					if (tmp[0][1] < -3000)
						tmp[0][1] = 0;
				}
				__except (EXCEPTION_EXECUTE_HANDLER)
				{
					DWORD err = GetExceptionCode();
					if (err == EXCEPTION_ACCESS_VIOLATION)
					{
						tmp[0][1] = 0;
						//std::cout << 111;
						//continue;

					}
					//}
				}
				__try
				{
					tmp[0][2] = A[i - 1][j + 1];
					if (tmp[0][2] < -3000)
						tmp[0][2] = 0;
				}
				__except (EXCEPTION_EXECUTE_HANDLER)
				{
					DWORD err = GetExceptionCode();
					if (err == EXCEPTION_ACCESS_VIOLATION)
					{
						tmp[0][2] = 0;
						//std::cout << 111;
						//continue;

					}
					//}
				}
				__try
				{
					tmp[1][0] = A[i][j - 1];
					if (tmp[1][0] < -3000)
						tmp[1][0] = 0;
				}
				__except (EXCEPTION_EXECUTE_HANDLER)
				{
					DWORD err = GetExceptionCode();
					if (err == EXCEPTION_ACCESS_VIOLATION)
					{
						tmp[1][0] = 0;
						//std::cout << 111;
						//continue;

					}
					//}
				}
				__try
				{
					tmp[1][1] = A[i][j];
					if (tmp[1][1] < -3000)
						tmp[1][1] = 0;
				}
				__except (EXCEPTION_EXECUTE_HANDLER)
				{
					DWORD err = GetExceptionCode();
					if (err == EXCEPTION_ACCESS_VIOLATION)
					{
						tmp[1][1] = 0;
						//std::cout << 111;
						//continue;

					}
					//}
				}
				__try
				{
					tmp[1][2] = A[i][j + 1];
					if (tmp[1][2] < -3000)
						tmp[1][2] = 0;
				}
				__except (EXCEPTION_EXECUTE_HANDLER)
				{
					DWORD err = GetExceptionCode();
					if (err == EXCEPTION_ACCESS_VIOLATION)
					{
						tmp[1][2] = 0;
						//std::cout << 111;
						//continue;

					}
					//}
				}
				__try
				{
					tmp[2][0] = A[i + 1][j - 1];
					if (tmp[2][0] < -3000)
						tmp[2][0] = 0;
				}
				__except (EXCEPTION_EXECUTE_HANDLER)
				{
					DWORD err = GetExceptionCode();
					if (err == EXCEPTION_ACCESS_VIOLATION)
					{
						tmp[2][0] = 0;
						//std::cout << 111;
						//continue;

					}
					//}
				}
				__try
				{
					tmp[2][1] = A[i + 1][j];
					if (tmp[2][1] < -3000)
						tmp[2][1] = 0;
				}
				__except (EXCEPTION_EXECUTE_HANDLER)
				{
					DWORD err = GetExceptionCode();
					if (err == EXCEPTION_ACCESS_VIOLATION)
					{
						tmp[2][1] = 0;
						//std::cout << 111;
						//continue;

					}
					//}
				}
				__try
				{
					tmp[2][2] = A[i + 1][j + 1];
					if (tmp[2][2]<-30000)
					{
						tmp[2][2] = 0;
					}
				}
				__except (EXCEPTION_EXECUTE_HANDLER)
				{
					DWORD err = GetExceptionCode();
					if (err == EXCEPTION_ACCESS_VIOLATION)
					{
						tmp[2][2] = 0;
						//std::cout << 111;
						//continue;

					}
					//}
				}
				//std::cout << tmp[1][1] << " " << tmp[2][2] << " ";
			//	std::cin.get();
				int tmp_mass[9] = { NULL };
				int size = K*L;
				int kk = 0;
				int tmp3;
			//	while (kk < size)
				//{
					for (int i = 0; i < K; i++)
					{
						for (int j = 0; j < L; j++)
						{
							tmp_mass[i*K+j] = tmp[i][j];

						}
					}
				//	kk++;
				//}
				//	std::cout <<tmp[1][1]<<" " << tmp_mass[4] << " " << tmp_mass[8] << " " << tmp[2][2] << " ";
				for (int i = 0; i < 9 - 1; i++)
				{
					for (int j = i + 1; j < 9; j++)
					{
						if (tmp_mass[i] < tmp_mass[j])
						{
							tmp3 = tmp_mass[i];
							tmp_mass[i] = tmp_mass[j];
							tmp_mass[j] = tmp3;
						}
					}
				//	std::cin.get();
				}
				int cf = 0;
				for (int i = 0; i < 9; i++)
				{
					if (tmp_mass[i] != 0)
						cf++;
				}
				int* tmp_mass2;
				tmp_mass2 = new int[cf];
				for (int i = 0; i < cf; i++)
				{
					tmp_mass2[i] = tmp_mass[i];
				}
				/*for (int i = 0; i < 9; i++)
				{
					std::cout << tmp_mass[i]<< " ";
				}
				//std::cout << cf << " " <<tmp_mass[cf / 2];
				std::cin.get();
				std::cin.get();*/
			//	int** ab = NULL;
				/*int c = 0;
				for (int i = 0; i < 9; i++)
				{
					if (tmp_mass[i] == 0)
					{
						continue;
					}
					else
						c++;
				}*/
			//std::cout << cf << " " ;
				if (cf % 2 == 0)
				{
					double b,c;
					b = (double)((double)(tmp_mass[cf / 2] + (double)tmp_mass[cf / 2 - 1]) / 2);
					//std::cout << b << std::endl;
					c = round1(b);
					A[i][j] = (int)c;
				}
				if (cf % 2 != 0) 
					A[i][j]=(tmp_mass[cf / 2 + cf % 2]);
				//std::cout << A[i][j]<< " ";
				//std::cout << tmp_mass[0] << " " << tmp_mass[1] << " " << tmp_mass[2] << " " << tmp_mass[5] << " ";
			}
		}
	}

	return A;
}